﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Threading;

namespace WpfBackgroundWorker
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private BackgroundWorker _bw = new BackgroundWorker();
        private bool _isPaused = false;
        public MainWindow()
        {
            InitializeComponent();

            progrss.Minimum = 0;
            progrss.Maximum = 99;

            _bw.WorkerReportsProgress = true;
            _bw.WorkerSupportsCancellation = true;

            _bw.DoWork += _bw_DoWork;
            _bw.ProgressChanged += _bw_ProgressChanged;
            _bw.RunWorkerCompleted += _bw_RunWorkerCompleted;
        }

        private void _bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (_isPaused)
            {
                progrss.Value = 0;
                buttonStart.Content = "Start";
                _isPaused = !_isPaused;
            }
            
        }

        private void _bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progrss.Value = e.ProgressPercentage;
        }

        private void _bw_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker bw = sender as BackgroundWorker;
            int start = (int)e.Argument;

            for (int i = start; i < 100; i++)
            {
                if (bw.CancellationPending)
                {
                    e.Cancel = true;
                    break;
                }
                Thread.Sleep(50);
                bw.ReportProgress(i);
            }
        }

        

        private void buttonStart_Click(object sender, RoutedEventArgs e)
        {
            if(!_bw.IsBusy)
            {
                _bw.RunWorkerAsync((int)progrss.Value);
            }
            
            _isPaused = !_isPaused;

            if (_isPaused)
            {
                buttonStart.Content = "Pause";
            }
            else
            {
                buttonStart.Content = "Continue";
                if (_bw.WorkerSupportsCancellation)
                {
                    _bw.CancelAsync();
                }
            }
        }

        private void buttonCancel_Click(object sender, RoutedEventArgs e)
        {
            if (_bw.WorkerSupportsCancellation)
            {
                _bw.CancelAsync();
                progrss.Value = 0;
                buttonStart.Content = "Start";
            }
        }
    }
}
